package com.sdry.service.impl.llm;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.sdry.mapper.llm.ClassesMapper;
import com.sdry.model.llm.Classes;
import com.sdry.service.llm.ClassesService;

/**
 * 
 * @Title:ClassesServiceImp
 * @Package com.sdry.service.impl.llm
 * @author llm
 * @date 2019年1月28日
 * @version 1.0
 */

@Service
public class ClassesServiceImp implements ClassesService {

	@Resource
	private ClassesMapper classesMapper;
	
	/**    
	 * 分页查询班次信息   
	 * @param map
	 * @return    
	 */
	@Override
	public List<Classes> listPageClasses(String className) {
		return classesMapper.listPageClasses(className);
	}

	/**      
	 * 保存班次信息 
	 * @param classes
	 * @return    
	 */
	@Override
	public Integer saveClasses(Classes classes) {
		int isSuccess = -2;
		List<Classes> list = classesMapper.listPageClasses(classes.getClassName());
		//判定班次名称是否已存在
		if(list.size() > 0){
			isSuccess = 1;
		}else{
			int i = classesMapper.saveClasses(classes);
			if(i > 0){
				isSuccess = 2;
			}
		}
		return isSuccess;
	}

	/** 
	 * 修改班次信息      
	 * @param classes
	 * @return    
	 */
	@Override
	public Integer updateClasses(Classes classes) {
		int isSuccess = -3;
		int i = 0;
		if(null != classes.getClassName() && !"".equals(classes.getClassName())){
			Classes c = classesMapper.getClassesById(classes.getCid());
			//判定班次名称是否发生修改
			if(c.getClassName().equals(classes.getClassName())){
				i = classesMapper.updateClasses(classes);
			}else{
				//判定修改后的班次名称是否已存在
				List<Classes> list = classesMapper.getClassesByName(classes.getClassName());
				if(list.size() > 0){
					isSuccess = 0;
				}else{
					i = classesMapper.updateClasses(classes);
				}
			}
			if(i > 0){
				isSuccess = 3;
			}
		}
		return isSuccess;
	}

	/**    
	 * 删除班次信息   
	 * @param cids
	 * @return    
	 */
	@Transactional(rollbackFor={Exception.class})
	public Integer deleteClasses(String str) {
		int isSuccess = 0;
		String[] split = str.split(",");
		try {
			for (String cid : split) {
				isSuccess = classesMapper.deleteClasses(Long.parseLong(cid));
			}
		}catch (
		Exception e) {
			e.printStackTrace();
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		}
		return isSuccess;
	}

	/**       
	 * @param name
	 * @return    
	 */
	@Override
	public List<Classes> getClassesByName(String name) {
		return classesMapper.getClassesByName(name);
	}
	
}
